package SwingView.Assignment;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jdesktop.swingx.JXDatePicker;

import SwingController.CmsPresenter;
import SwingModel.CmsAssignment;
import SwingView.CmsContentPanelModel;
import SwingView.CmsTimeSelector;
import SwingView.CmsWeightSelector;

public class CmsCPAsgmComposer  extends CmsContentPanelModel{

	private final JLabel jlAsgmComposerTitle;
	private final JPanel jpContent;
	private JTextField jtfTitle;
	private JTextArea jtfDescription;
	private JButton jbtClear;
	private JButton jbtPost;
	
	//AvaiDate:
	private JCheckBox avaiDateCB;
	private JXDatePicker avaiDatePicker;
	private CmsTimeSelector avaiTimeSelector;
	
	//DueDate:
	private JCheckBox dueDateCB;
	private JXDatePicker dueDatePicker;
	private CmsTimeSelector dueTimeSelector;
	
	//ExpDate:
	private JCheckBox expDateCB;
	private JXDatePicker expDatePicker;
	private CmsTimeSelector expTimeSelector;
	
	
	
	//Questions
	private JPanel jpFiles;
	private List<FileEntry> listOfFilePanel;
	private CmsWeightSelector weightSelector;
	
	public CmsCPAsgmComposer(){
		listOfFilePanel = new ArrayList();
		
		//helper panel to center the title panel
		JPanel jp = new JPanel();
		jp.setPreferredSize(new Dimension(650, 65));
		jp.setBorder(BorderFactory.createEmptyBorder());
		GridBagLayout gbl_jp = new GridBagLayout();
		jp.setLayout(gbl_jp);
		
		//content panel
		jpContent = new JPanel();
		GridBagLayout gbl_jpContent = new GridBagLayout();
		jpContent.setLayout(gbl_jpContent);
		jpContent.setOpaque(false);

		//Set up the scroll bar
		JScrollPane scroll = new JScrollPane(jpContent);
		
		//main Scroll bar
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setOpaque(true);
		scroll.setPreferredSize(new Dimension(650,450));
		scroll.setBackground(Color.WHITE);

		//set up main panel and add components
		setLayout(new BorderLayout());
		setOpaque(false);
		add(jp,BorderLayout.NORTH);//Add title People
		
		//instantiate Quiz title
		jlAsgmComposerTitle = new JLabel("Assignment Composer");
		jlAsgmComposerTitle.setFont(jlAsgmComposerTitle.getFont().deriveFont(20.0f).deriveFont(Font.BOLD));
		jlAsgmComposerTitle.setBorder(BorderFactory.createEmptyBorder(0, 8, 10, 8));
		GridBagConstraints gbc_jlQuizComposerTitle = new GridBagConstraints();
		gbc_jlQuizComposerTitle.gridwidth = 2;
		gbc_jlQuizComposerTitle.gridx = 0;
		gbc_jlQuizComposerTitle.gridy = 0;
		jp.add(jlAsgmComposerTitle, gbc_jlQuizComposerTitle);
		
		jbtClear = new JButton("Clear Everything");
		jbtClear.setFont(jbtClear.getFont().deriveFont(Font.BOLD).deriveFont(13.0f));
		jbtClear.setPreferredSize(new Dimension(140, 20));
		jbtClear.setMinimumSize(new Dimension(75,20));
		jbtClear.setBackground(new Color(170,170,170));
		jbtClear.setForeground(new Color(30,30,30));
		jbtClear.setOpaque(true);
		jbtClear.setBorder(BorderFactory.createLineBorder(new Color(30,30,30)));
		GridBagConstraints gbc_jbtClear = new GridBagConstraints();
		gbc_jbtClear.insets = new Insets(0, 0, 0, 20);
		gbc_jbtClear.gridx = 0;
		gbc_jbtClear.gridy = 1;
		jp.add(jbtClear, gbc_jbtClear);
		
		jbtPost = new JButton("Post");
		jbtPost.setFont(jbtPost.getFont().deriveFont(Font.BOLD).deriveFont(13.0f));
		jbtPost.setPreferredSize(new Dimension(70, 20));
		jbtPost.setMinimumSize(new Dimension(75,20));
		jbtPost.setBackground(new Color(170,170,170));
		jbtPost.setForeground(new Color(30,30,30));
		jbtPost.setOpaque(true);
		jbtPost.setBorder(BorderFactory.createLineBorder(new Color(30,30,30)));
		GridBagConstraints gbc_jbtPost = new GridBagConstraints();
		gbc_jbtPost.insets = new Insets(0, 20, 0, 0);
		gbc_jbtPost.gridx = 1;
		gbc_jbtPost.gridy = 1;
		jp.add(jbtPost, gbc_jbtPost);
		add(scroll,BorderLayout.WEST);//Add People List
		
		//_________________________________________________________
		//TITLE:
		JPanel jpTitle = new JPanel();
		jpTitle.setOpaque(false);
		jpTitle.setPreferredSize(new Dimension(550,30));
		jpTitle.setMinimumSize(new Dimension(550,30));
		GridBagConstraints gbc_jpTitle = new GridBagConstraints();
		gbc_jpTitle.insets = new Insets(0, 0, 5, 0);
		gbc_jpTitle.fill = GridBagConstraints.HORIZONTAL;
		gbc_jpTitle.gridx = 0;
		gbc_jpTitle.gridy = 0;
		jpContent.add(jpTitle, gbc_jpTitle);
		GridBagLayout gbl_jpTitle = new GridBagLayout();
		jpTitle.setLayout(gbl_jpTitle);
		
		JLabel jlTitle = new JLabel("Title: ");
		jlTitle.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_jlTitle = new GridBagConstraints();
		gbc_jlTitle.weightx = 1.0;
		gbc_jlTitle.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_jlTitle.insets = new Insets(0, 0, 0, 5);
		gbc_jlTitle.gridx = 0;
		gbc_jlTitle.gridy = 0;
		jpTitle.add(jlTitle, gbc_jlTitle);
		
		jtfTitle = new JTextField();
		jtfTitle.setToolTipText("Please enter the assignment title");
		jtfTitle.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		jtfTitle.setMinimumSize(new Dimension(430,20));
		jtfTitle.setPreferredSize(new Dimension(430,20));
		GridBagConstraints gbc_jtfTitle = new GridBagConstraints();
		gbc_jtfTitle.anchor = GridBagConstraints.FIRST_LINE_END;
		gbc_jtfTitle.gridx = 1;
		gbc_jtfTitle.gridy = 0;
		jpTitle.add(jtfTitle, gbc_jtfTitle);
		
		//_________________________________________________________
		//DESCRIPTION:
		JPanel jpDescription = new JPanel();
		jpDescription.setPreferredSize(new Dimension(550, 100));
		jpDescription.setMinimumSize(new Dimension(550, 100));
		GridBagLayout gbl_jpDescription = new GridBagLayout();
		gbl_jpDescription.rowWeights = new double[]{0.0};
		gbl_jpDescription.columnWeights = new double[]{1.0, 0.0};
		jpDescription.setLayout(gbl_jpDescription);
		jpDescription.setOpaque(false);
		GridBagConstraints gbc_jpDescription = new GridBagConstraints();
		gbc_jpDescription.insets = new Insets(0, 0, 5, 0);
		gbc_jpDescription.fill = GridBagConstraints.HORIZONTAL;
		gbc_jpDescription.gridx = 0;
		gbc_jpDescription.gridy = 1;
		jpContent.add(jpDescription, gbc_jpDescription);
		
		JLabel jlDescription = new JLabel("Description: ");
		jlDescription.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_jlDescription = new GridBagConstraints();
		gbc_jlDescription.insets = new Insets(0, 0, 5, 5);
		gbc_jlDescription.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_jlDescription.gridx = 0;
		gbc_jlDescription.gridy = 0;
		gbc_jlDescription.weightx = 1.0;
		jpDescription.add(jlDescription, gbc_jlDescription);
		
		jtfDescription = new JTextArea();
		jtfDescription.setLineWrap(true);
		jtfDescription.setWrapStyleWord(true);
		
		JScrollPane jspDescription = new JScrollPane(jtfDescription);
		jspDescription.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		jspDescription.setPreferredSize(new Dimension(430,90));
		GridBagConstraints gbc_jspDescription = new GridBagConstraints();
		gbc_jspDescription.insets = new Insets(0, 0, 5, 0);
		gbc_jspDescription.fill = GridBagConstraints.BOTH;
		gbc_jspDescription.gridx = 1;
		gbc_jspDescription.gridy = 0;
		jpDescription.add(jspDescription, gbc_jspDescription);
		//_________________________________________________________
		//AVAI DATE:
		JLabel lblAvailableDate = new JLabel("Available Date:");
		lblAvailableDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblAvailableDate = new GridBagConstraints();
		gbc_lblAvailableDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblAvailableDate.insets = new Insets(0, 0, 5, 0);
		gbc_lblAvailableDate.gridx = 0;
		gbc_lblAvailableDate.gridy = 2;
		jpContent.add(lblAvailableDate, gbc_lblAvailableDate);
		
		JPanel jpAvaiDate = new JPanel();
		jpAvaiDate.setOpaque(false);
		FlowLayout fl_jpAvaiDate = (FlowLayout) jpAvaiDate.getLayout();
		fl_jpAvaiDate.setVgap(0);
		fl_jpAvaiDate.setAlignment(FlowLayout.LEFT);
		GridBagConstraints gbc_jpAvaiDate = new GridBagConstraints();
		gbc_jpAvaiDate.insets = new Insets(0, 0, 5, 0);
		gbc_jpAvaiDate.fill = GridBagConstraints.BOTH;
		gbc_jpAvaiDate.gridx = 0;
		gbc_jpAvaiDate.gridy = 3;
		jpContent.add(jpAvaiDate, gbc_jpAvaiDate);
		
		avaiDateCB = new JCheckBox("");
		avaiDateCB.setSelected(true);
		jpAvaiDate.add(avaiDateCB);
		
		avaiDatePicker = new JXDatePicker();
		avaiDatePicker.setDate(new Date());
		jpAvaiDate.add(avaiDatePicker);
		
		avaiTimeSelector = new CmsTimeSelector();
		jpAvaiDate.add(avaiTimeSelector);
		
		//_________________________________________________________
		//DUE DATE:
		JLabel lblDueDate = new JLabel("Due Date:");
		lblDueDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblDueDate = new GridBagConstraints();
		gbc_lblDueDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblDueDate.insets = new Insets(0, 0, 5, 0);
		gbc_lblDueDate.gridx = 0;
		gbc_lblDueDate.gridy = 4;
		jpContent.add(lblDueDate, gbc_lblDueDate);
		
		JPanel jpDueDate = new JPanel();
		jpDueDate.setOpaque(false);
		FlowLayout fl_jpDueDate = (FlowLayout) jpDueDate.getLayout();
		fl_jpDueDate.setVgap(0);
		fl_jpDueDate.setAlignment(FlowLayout.LEFT);
		GridBagConstraints gbc_jpDueDate = new GridBagConstraints();
		gbc_jpDueDate.insets = new Insets(0, 0, 5, 0);
		gbc_jpDueDate.fill = GridBagConstraints.BOTH;
		gbc_jpDueDate.gridx = 0;
		gbc_jpDueDate.gridy = 5;
		jpContent.add(jpDueDate, gbc_jpDueDate);
		
		dueDateCB = new JCheckBox("");
		dueDateCB.setSelected(true);
		dueDateCB.setEnabled(false);
		jpDueDate.add(dueDateCB);
		
		dueDatePicker = new JXDatePicker();
		dueDatePicker.setDate( new Date((new Date()).getTime() + 7 * 24 * 60 * 60 * 1000)); //Default to be a week from now
		jpDueDate.add(dueDatePicker);
		
		dueTimeSelector = new CmsTimeSelector();
		jpDueDate.add(dueTimeSelector);
		
		//_________________________________________________________
		//EXP DATE:
		JLabel lblExpirationDate = new JLabel("Expiration Date: ");
		lblExpirationDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblExpirationDate = new GridBagConstraints();
		gbc_lblExpirationDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblExpirationDate.insets = new Insets(0, 0, 5, 0);
		gbc_lblExpirationDate.gridx = 0;
		gbc_lblExpirationDate.gridy = 6;
		jpContent.add(lblExpirationDate, gbc_lblExpirationDate);
		
		JPanel jpExpDate = new JPanel();
		jpExpDate.setOpaque(false);
		FlowLayout flowLayout = (FlowLayout) jpExpDate.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		GridBagConstraints gbc_jpExpDate = new GridBagConstraints();
		gbc_jpExpDate.insets = new Insets(0, 0, 5, 0);
		gbc_jpExpDate.fill = GridBagConstraints.BOTH;
		gbc_jpExpDate.gridx = 0;
		gbc_jpExpDate.gridy = 7;
		jpContent.add(jpExpDate, gbc_jpExpDate);
		
		expDateCB = new JCheckBox();
		expDateCB.setSelected(false);
		jpExpDate.add(expDateCB);
		
		expDatePicker = new JXDatePicker();
		expDatePicker.setDate(new Date(CmsPresenter.END_OF_SEMESTER.getTime()));
		expDatePicker.setEnabled(false);
		jpExpDate.add(expDatePicker);
		
		expTimeSelector = new CmsTimeSelector();
		expTimeSelector.setEnabled(false);
		jpExpDate.add(expTimeSelector);
		
		//_________________________________________________________
		//PROPERTIES:
		JPanel jpProp = new JPanel();
		jpProp.setOpaque(false);
		GridBagConstraints gbc_jpProp = new GridBagConstraints();
		gbc_jpProp.insets = new Insets(0, 0, 5, 0);
		gbc_jpProp.fill = GridBagConstraints.HORIZONTAL;
		gbc_jpProp.gridx = 0;
		gbc_jpProp.gridy = 8;
		jpContent.add(jpProp, gbc_jpProp);
		GridBagLayout gbl_jpProp = new GridBagLayout();
		jpProp.setLayout(gbl_jpProp);
		
		JLabel jlWeight = new JLabel("Percent Weight: ");
		jlWeight.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_jlWeight = new GridBagConstraints();
		gbc_jlWeight.anchor = GridBagConstraints.LINE_START;
		gbc_jlWeight.fill = GridBagConstraints.HORIZONTAL;
		gbc_jlWeight.insets = new Insets(0, 0, 5, 5);
		gbc_jlWeight.gridx = 0;
		gbc_jlWeight.gridy = 0;
		jpProp.add(jlWeight, gbc_jlWeight);
		
		weightSelector = new CmsWeightSelector();
		GridBagConstraints gbc_jsWeight = new GridBagConstraints();
		gbc_jsWeight.insets = new Insets(0, 0, 5, 5);
		gbc_jsWeight.gridx = 1;
		gbc_jsWeight.gridy = 0;
		gbc_jsWeight.weightx = 1.0;
		gbc_jsWeight.anchor = GridBagConstraints.LINE_START;
		jpProp.add(weightSelector, gbc_jsWeight);
		
		//_________________________________________________________
		//QUESTION:
		JPanel jpQ = new JPanel();
		jpQ.setOpaque(false);
		GridBagConstraints gbc_jpQ = new GridBagConstraints();
		gbc_jpQ.fill = GridBagConstraints.HORIZONTAL;
		gbc_jpQ.gridx = 0;
		gbc_jpQ.gridy = 9;
		jpContent.add(jpQ, gbc_jpQ);
		GridBagLayout gbl_jpQ = new GridBagLayout();
		jpQ.setLayout(gbl_jpQ);
		
		//Question Label
		JLabel lblFiles = new JLabel("Files:");
		lblFiles.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblQuestions = new GridBagConstraints();
		gbc_lblQuestions.insets = new Insets(0, 0, 10, 0);
		gbc_lblQuestions.weightx = 0.05;
		gbc_lblQuestions.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblQuestions.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_lblQuestions.gridx = 0;
		gbc_lblQuestions.gridy = 0;
		jpQ.add(lblFiles, gbc_lblQuestions);
		
		//Add Question button
		JButton jbtAddFile = new JButton("+ Add File");
		
		jbtAddFile.setOpaque(false);
		jbtAddFile.setBorder(BorderFactory.createEmptyBorder());
		jbtAddFile.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		jbtAddFile.setForeground(Color.BLUE);
		GridBagConstraints gbc_jbtAddQuestion = new GridBagConstraints();
		gbc_jbtAddQuestion.weightx = 0.5;
		gbc_jbtAddQuestion.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_jbtAddQuestion.gridx = 1;
		gbc_jbtAddQuestion.gridy = 0;
		jpQ.add(jbtAddFile, gbc_jbtAddQuestion);
		
		//remove question button
		jpFiles = new JPanel();
		jpFiles.setOpaque(false);
		GridBagConstraints gbc_jpQuestions = new GridBagConstraints();
		gbc_jpQuestions.weighty = 1.0;
		gbc_jpQuestions.fill = GridBagConstraints.HORIZONTAL;
		gbc_jpQuestions.gridx = 0;
		gbc_jpQuestions.gridy = 10;
		jpContent.add(jpFiles, gbc_jpQuestions);
		jpFiles.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		//ACTION LISTENERS:
		jbtAddFile.addActionListener(new AddFileActionListener());
		
		//AvaiDate Checkbox action listener:
		avaiDateCB.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				if(avaiDateCB.isSelected()){
					avaiDatePicker.setEnabled(true);
					avaiTimeSelector.setEnabled(true);
				}
				else{
					avaiDatePicker.setEnabled(false);
					avaiTimeSelector.setEnabled(false);
				}
			}
			
		});
		
		//AvaiDate value Check
		avaiDatePicker.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				validateDateInput();
			}
			
		});
		
		//AvaiDate value Check
		avaiDatePicker.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				validateDateInput();
			}
			
		});
		
		//avai TimeSelector value check
		avaiTimeSelector.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				validateDateInput();
			}
		});
		
		//Due Checkbox action listener:
		dueDateCB.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				if(dueDateCB.isSelected()){
					dueDatePicker.setEnabled(true);
					dueTimeSelector.setEnabled(true);
				}
				else{
					dueDatePicker.setEnabled(false);
					dueTimeSelector.setEnabled(false);
				}
			}
			
		});
		
		//DueDate value Check
		dueDatePicker.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				validateDateInput();
			}
			
		});
		
		//due TimeSelector value check
		dueTimeSelector.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				validateDateInput();
			}
		});
		
		//Exp Checkbox action listener:
		expDateCB.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				if(expDateCB.isSelected()){
					expDatePicker.setEnabled(true);
					expTimeSelector.setEnabled(true);
				}
				else{
					expDatePicker.setEnabled(false);
					expTimeSelector.setEnabled(false);
				}
			}
			
		});
		
		//ExpDate value Check
		expDatePicker.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				validateDateInput();
			}
			
		});
		
		//exp TimeSelector value check
		expTimeSelector.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				validateDateInput();
			}
		});
		
		//clear action listner
		jbtClear.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				int value = JOptionPane.showConfirmDialog(CmsPresenter.mainView, "Do you want to clear all of your inputs?",
															"WARNING", JOptionPane.WARNING_MESSAGE);
				
				if(value==JOptionPane.YES_OPTION) clearAllInput();
			}
			
		});
		
		//post action listener
		jbtPost.addActionListener(new ALAssignmentPost());
	}
	
	private void displayListOfFile(){
		jpFiles.removeAll();
		for(int i=0; i < listOfFilePanel.size(); i++){
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = i;
			jpFiles.add(listOfFilePanel.get(i),c); //add to panel for displaying
			System.out.println(i);
		}
		jpFiles.revalidate();
	}
	
	public Date getSelectedAvaiDate(){
		Date avaiDate;
		
		if(avaiDateCB.isSelected()){
			long dayMilis = avaiDatePicker.getDate().getTime();
			long timeMilis = avaiTimeSelector.getHour() * 60 * 60 * 1000 //milisecond = hour * 60min * 60sec * 1000milis
					+ avaiTimeSelector.getMinute() * 60 * 1000;//milisecond = min * 60sec * 1000milis
			avaiDate = new Date(dayMilis + timeMilis);
		}
		else avaiDate = new Date();
		
		return avaiDate;	
	}
	
	public Date getSelectedExpDate(){
		Date expDate;
		
		if(this.expDateCB.isSelected()){
			long dayMilis = expDatePicker.getDate().getTime();
			long timeMilis = expTimeSelector.getHour() * 60 * 60 * 1000 //milisecond = hour * 60min * 60sec * 1000milis
					+ expTimeSelector.getMinute() * 60 * 1000;//milisecond = min * 60sec * 1000milis
			expDate = new Date(dayMilis + timeMilis);
		}
		else expDate = new Date(CmsPresenter.DEFAULT_EXP_DATE.getTime());
		
		return expDate;
	}
	
	public Date getSelectedDueDate(){
		Date dueDate;
		
		if(this.dueDateCB.isSelected()){
			long dayMilis = dueDatePicker.getDate().getTime();
			long timeMilis = dueTimeSelector.getHour() * 60 * 60 * 1000 //milisecond = hour * 60min * 60sec * 1000milis
					+ dueTimeSelector.getMinute() * 60 * 1000;//milisecond = min * 60sec * 1000milis
			dueDate = new Date(dayMilis + timeMilis);
		}
		else dueDate = new Date(CmsPresenter.DEFAULT_EXP_DATE.getTime());
		
		return dueDate;
	}
	
	public String getTitle(){
		return jtfTitle.getText();
	}
	
	public String getDescription(){
		return jtfDescription.getText();
	}
	
	public double getWeight(){
		return weightSelector.getWeight();
	}
	
	
	public CmsAssignment getComposedAssignment(){
		int asgmID = 0;
		String asgmTitle = getTitle();
		String asgmDescription = getDescription();
		List<File> asgmFile = new ArrayList(); 
		for(FileEntry f : listOfFilePanel){
			asgmFile.add(f.getSelectedFile());
		}
		double percentWeight = getWeight();
		Date createdDate = new Date();
		Date avaiDate = getSelectedAvaiDate();
		Date dueDate = getSelectedDueDate();
		Date expDate = getSelectedExpDate();
		return new CmsAssignment(asgmID, asgmTitle, asgmDescription, asgmFile, percentWeight, createdDate, avaiDate, dueDate, expDate);
	}
	
	
	public void clearAllInput(){
		jtfTitle.setText("");
		jtfDescription.setText("");
		avaiDateCB.setSelected(true);
		avaiDatePicker.setDate(new Date());
		avaiTimeSelector.setTime(0, 0);
		dueDatePicker.setDate( new Date((new Date()).getTime() + 7 * 24 * 60 * 60 * 1000));
		expDateCB.setSelected(false);
		expDatePicker.setDate(new Date(CmsPresenter.END_OF_SEMESTER.getTime()));
		expTimeSelector.setTime(0, 0);
		weightSelector.reset();
		listOfFilePanel = new ArrayList();
		jpFiles.removeAll();
		jpFiles.revalidate();
		jpFiles.repaint();
	}
	
	public boolean validateAllInput(){
		if(validateTitleInput() & validateDateInput()){
			if(listOfFilePanel.size() == 0){
				int returnVal = JOptionPane.showConfirmDialog(CmsPresenter.mainView, 
								"You have not selected any file to attach with this assignment.\n\n Do you want to continue posting this assignment?");
				if(returnVal == JOptionPane.YES_OPTION) {
					if(weightSelector.getWeight() == 0.0){
						int returnVal2 = JOptionPane.showConfirmDialog(CmsPresenter.mainView, "The percent weight for this assignment is 0.0%.\nDo you want to continue posting this quiz?");
						if(returnVal2 == JOptionPane.YES_OPTION) return true;
						else return false;
					}
					else return true;
				}
				else return false;
				
			}
			else{
				if(weightSelector.getWeight() == 0.0){
					int returnVal2 = JOptionPane.showConfirmDialog(CmsPresenter.mainView, "The percent weight for this assignment is 0.0%.\nDo you want to continue posting this quiz?");
					if(returnVal2 == JOptionPane.YES_OPTION) return true;
					else return false;
				}
				else return true;
			}
		}
		return false;
	}
	
	public boolean validateDateInput(){
		Date avaiDate = new Date(getSelectedAvaiDate().getTime());
		Date dueDate = new Date(getSelectedDueDate().getTime());
		Date expDate = new Date(getSelectedExpDate().getTime());
		if(avaiDate.compareTo(dueDate) >= 0){
			JOptionPane.showMessageDialog(CmsPresenter.mainView,
				    "The available Date and Time have to be before the due Date and Time!",
				    "ERROR!",
				    JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else if(dueDate.compareTo(expDate) >= 0){
			JOptionPane.showMessageDialog(CmsPresenter.mainView,
				    "The due Date and Time have to be before the expiration Date and Time!",
				    "ERROR!",
				    JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else if(dueDate.compareTo(new Date()) <= 0){
			JOptionPane.showMessageDialog(CmsPresenter.mainView,
				    "Due Date has to be before today",
				    "ERROR!",
				    JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else if(expDate.compareTo(new Date()) <= 0){
			JOptionPane.showMessageDialog(CmsPresenter.mainView,
				    "Exp Date has to be before today",
				    "ERROR!",
				    JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else return true;
	}
	
	public boolean validateTitleInput(){
		if(jtfTitle.getText().equals("")){
			JOptionPane.showMessageDialog(CmsPresenter.mainView,
				    "Please input a Title for the Assignment!",
				    "ERROR!",
				    JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else return true;
	}
	
	public boolean validateFileInput(File newFile){
		
		//compare new file's name to all selected files
		for(FileEntry fe : listOfFilePanel){
			if(fe.getSelectedFile().getName().equals(newFile.getName())){
				String errorMsg = new String("ERROR: Your selected File, "
											+newFile.getName() 
											+ ", is already inserted on the file list");
						
				JOptionPane.showMessageDialog(CmsPresenter.mainView,errorMsg, "ERROR",JOptionPane.ERROR_MESSAGE);
				return false;
			}
		}
		
		return true;
		
	}
	
	class AddFileActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			JFileChooser fc = new JFileChooser();
			int returnVal = fc.showOpenDialog(CmsPresenter.mainView);
			if(returnVal == JFileChooser.APPROVE_OPTION){
				File selectedFile = fc.getSelectedFile();
				if(validateFileInput(selectedFile)){
					listOfFilePanel.add(new FileEntry(selectedFile));
					displayListOfFile();
				}
			}
		}
		
	}
	
	public class FileEntry extends JPanel{
		
		private static final int HEIGHT = 60;
		private static final int WIDTH = 500;
		
		private File selectedFile;
		
		private JLabel jlFileName;
		private JButton selectButton;
		private JButton removeButton;
		
		public FileEntry(File file) {
			this.selectedFile = file;
			
			setBackground(Color.WHITE);
			
			GridBagLayout gridBagLayout = new GridBagLayout();
			setLayout(gridBagLayout);
			setPreferredSize(new Dimension(WIDTH,HEIGHT));
			setMaximumSize(new Dimension(WIDTH,HEIGHT));
			setMinimumSize(new Dimension(WIDTH,HEIGHT));
			setOpaque(false);
			setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
			
			JPanel panel = new JPanel();
			panel.setOpaque(false);
			panel.setPreferredSize(new Dimension(WIDTH-20,HEIGHT));
			panel.setMaximumSize(new Dimension(WIDTH-20,HEIGHT));
			panel.setMinimumSize(new Dimension(WIDTH-20,HEIGHT));
			panel.setBorder(BorderFactory.createEmptyBorder());
			GridBagConstraints gbc_panel = new GridBagConstraints();
			gbc_panel.anchor = GridBagConstraints.LINE_START;
			gbc_panel.insets = new Insets(10, 10, 5, 10);
			gbc_panel.fill = GridBagConstraints.HORIZONTAL;
			gbc_panel.gridx = 0;
			gbc_panel.gridy = 0;
			add(panel, gbc_panel);
			GridBagLayout gbl_panel = new GridBagLayout();
			panel.setLayout(gbl_panel);
			
			jlFileName = new JLabel(selectedFile.getName());
			jlFileName.setFont(new Font("Lucida Grande", Font.BOLD, 13));
			GridBagConstraints gbc_jlQuestionNum = new GridBagConstraints();
			gbc_jlQuestionNum.anchor = GridBagConstraints.LINE_START;
			gbc_jlQuestionNum.insets = new Insets(0, 0, 0, 5);
			gbc_jlQuestionNum.fill = GridBagConstraints.HORIZONTAL;
			gbc_jlQuestionNum.gridx = 0;
			gbc_jlQuestionNum.gridy = 0;
			gbc_jlQuestionNum.gridwidth = 2;
			panel.add(jlFileName, gbc_jlQuestionNum);
			
			selectButton = new JButton("Reselect File");
			selectButton.setForeground(Color.BLUE);
			selectButton.setOpaque(false);
			selectButton.setBorder(BorderFactory.createEmptyBorder());
			GridBagConstraints gbc_jbtClear = new GridBagConstraints();
			gbc_jbtClear.anchor = GridBagConstraints.LINE_END;
			gbc_jbtClear.weightx = 1.0;
			gbc_jbtClear.insets = new Insets(0, 0, 0, 30);
			gbc_jbtClear.gridx = 0;
			gbc_jbtClear.gridy = 1;
			panel.add(selectButton, gbc_jbtClear);
			
			removeButton = new JButton("Remove");
			removeButton.setForeground(Color.BLUE);
			removeButton.setOpaque(false);
			removeButton.setBorder(BorderFactory.createEmptyBorder());
			GridBagConstraints gbc_btnRemove = new GridBagConstraints();
			gbc_btnRemove.anchor = GridBagConstraints.LINE_END;
			gbc_btnRemove.gridx = 1;
			gbc_btnRemove.gridy = 1;
			panel.add(removeButton, gbc_btnRemove);
			
			
			//ACTION LISTENER:
			selectButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showOpenDialog(CmsPresenter.mainView);
					if(returnVal == JFileChooser.APPROVE_OPTION){
						
						if(validateFileInput(fc.getSelectedFile())){
							selectedFile = new File(fc.getSelectedFile().getPath());
							jlFileName.setText(selectedFile.getName());
						}
					}
				}
				
			});
			
			removeButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					listOfFilePanel.remove(FileEntry.this);
					displayListOfFile();
				}
				
			});
		}
		
		public File getSelectedFile(){
			return this.selectedFile;
		}
	}
}
